﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class admin_ManageMsg : System.Web.UI.Page
{
    Message m = new Message();
    int sumPage;

    protected void Page_Load(object sender, EventArgs e)
    {

 //       Function.CheckSession();

        if (!this.IsPostBack)
        {
            Function.CheckSession();
            MsgBind();
        }
    }

   

    void MsgBind()
    {
        int pageNo = 1;
        int pageSize = 5;
        if (Request.QueryString["page"] == null)
        {
            pageNo = 1;
        }
        else
        {
            pageNo = Int32.Parse(Request.QueryString["page"]);
        }


        SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING);
        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = new SqlCommand();
        da.SelectCommand.Connection = conn;
        da.SelectCommand.CommandText = "getdataset";
        da.SelectCommand.CommandType = CommandType.StoredProcedure;
        da.SelectCommand.Parameters.Add("@TableList", SqlDbType.VarChar, 200).Value = "*";
        da.SelectCommand.Parameters.Add("@TableName", SqlDbType.VarChar, 30).Value = "message";
        da.SelectCommand.Parameters.Add("@SelectWhere", SqlDbType.VarChar, 500).Value = " mid>0 ";
        da.SelectCommand.Parameters.Add("@SelectOrderId", SqlDbType.VarChar, 20).Value = "mid";
        da.SelectCommand.Parameters.Add("@SelectOrder", SqlDbType.VarChar, 200).Value = "order by mid desc";
        da.SelectCommand.Parameters.Add("@intPageNo", SqlDbType.Int).Value = pageNo;
        da.SelectCommand.Parameters.Add("@intPageSize", SqlDbType.Int).Value = pageSize;
        da.SelectCommand.Parameters.Add("@RecordCount", SqlDbType.Int).Direction = ParameterDirection.Output;
        da.SelectCommand.Parameters.Add("RowCount", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;
        DataSet ds = new DataSet();
        da.Fill(ds, "guestbook");
        this.DataList1.DataSource = ds;
        this.DataList1.DataBind();

        Int32 RecordCount = (Int32)da.SelectCommand.Parameters["@RecordCount"].Value; 
        Int32 RowCount = (Int32)da.SelectCommand.Parameters["RowCount"].Value; 
        lbRecord.Text = RecordCount.ToString();
        lbRow.Text = pageNo.ToString();
        sumPage = (Int32)RecordCount / pageSize;

        if (RecordCount % pageSize > 0)
        {
            sumPage = sumPage + 1;
        }
        lbpage.Text = sumPage.ToString();
        if (pageNo > 1)
        {
            hylfirst.NavigateUrl = "ManageMsg.aspx?page=1";
            hylprev.NavigateUrl = string.Concat("ManageMsg.aspx?page=", "", pageNo - 1);
        }
        else
        {
            hylprev.NavigateUrl = "";
            hylfirst.NavigateUrl = "";
            hylfirst.Visible = false;
            hylprev.Visible = false;
        }
        if (pageNo < sumPage)
        {
            hylend.NavigateUrl = string.Concat("ManageMsg.aspx?page=", "", sumPage);
            hylnext.NavigateUrl = string.Concat("ManageMsg.aspx?page=", "", pageNo + 1);
        }
        else
        {
            hylnext.NavigateUrl = "";
            hylend.NavigateUrl = "";
            hylend.Visible = false;
            hylnext.Visible = false;
        }
    }

    protected void link_Click(object sender, EventArgs e)
    {
        int page = Convert.ToInt32(txtlink.Text);
        if (page > sumPage)
        {
            Response.Redirect("ManageMsg.aspx");
        }
        else
        {
            Response.Redirect("ManageMsg.aspx?page=" + page + "");
        }
    }


    protected void Button2_Command(object sender, CommandEventArgs e)
    {
       
    }

    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName == "reply")
        {
            TextBox rcontent = (TextBox)e.Item.FindControl("TextBox2");//回复内容
            m.Mid = Convert.ToInt32(e.CommandArgument.ToString());
            m.Replycontent = rcontent.Text;
            m.Replytime = DateTime.Now;
            m.ReplyMsg(m);
            MsgBind();
            Response.Write("<script>alert('回复成功');</script>");
        }
        else if (e.CommandName == "update")
        {
            Response.Write("bbb");
        }
        else 
        {
            m.DelMsg(Convert.ToInt32(e.CommandArgument.ToString()));
            Response.Write("<script>alert('删除成功');</script>");
            MsgBind();
        }
    }
   
}
